package gw.com.android.ui.trade;

import android.app.Activity;
import android.text.TextUtils;

import gw.com.android.app.AppContances;
import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigUtil;
import gw.com.android.ui.dialog.PopupConfirmDialog;
import www.com.library.model.DataItemDetail;
import www.com.library.util.NetworkMonitor;
import gw.com.android.utils.ServerConnnectUtil;
import www.com.library.app.AppActivities;
import www.com.library.app.Logger;
import www.com.library.util.DoubleConverter;
import gw.com.jni.library.terminal.GTSConst;

/**
 * 下单时各状态，各范围值检查及范围计算，价格点数转换等逻辑处理
 * @author reeta
 *
 */
public class TradeBusiness {

	/**
	 * 检查是否可以开仓或平仓
	 * @param act 当前activity
	 * @param zoneType 产品板块信息
	 * @param Tradable_ 产品交易状态
	 * @param isOpenOrder 开仓还是平仓 true为开仓，false为平仓
	 * 
	 * @return
	 */

	public static boolean PreOrderCheck(Activity act, int zoneType, int Tradable_, boolean isOpenOrder) {
		return PreOrderCheck(act, zoneType, Tradable_, isOpenOrder, true);
	}

	/**
	 * 检查是否可以开仓或平仓
	 * @param act 当前activity
	 * @param zoneType 产品板块信息
	 * @param Tradable_ 产品交易状态
	 * @param isOpenOrder 开仓还是平仓 true为开仓，false为平仓
	 * @param checkNetWork 是否添加网络和服务器判断
     * @return
     */
	public static boolean PreOrderCheck(Activity act, int zoneType, int Tradable_, boolean isOpenOrder, boolean checkNetWork) {
		String errMsg = null;
		if (null == act) {
			act = AppActivities.getSingleton().currentActivity();
		}
		if(checkNetWork){
			if (!NetworkMonitor.hasNetWork()) {
				return false;
			}else if(!ServerConnnectUtil.instance().isTradeConnect || !ServerConnnectUtil.instance().isQuoteConnect){
				ServerConnnectUtil.instance().reConnectServerConfirm(act);
				return false;
			}
		}
		if(zoneType == GTSConst.ZONE_TYPE_REF){
			errMsg = ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1018);
		}else if (Tradable_ != AppContances.TRADE_STATU_ABLE) {
			if(Tradable_ == AppContances.TRADE_STATU_ENABLE){
				errMsg = ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1016);
			}else if(Tradable_ == AppContances.TRADE_STATU_NOT_TIME){
				errMsg = ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1017);
			}else if(Tradable_ == AppContances.TRADE_STATU_REF){
				errMsg = ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1018);
			}else if(Tradable_ == AppContances.TRADE_STATU_OVERDUR){
				errMsg = ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1019);
			}else if(isOpenOrder && Tradable_ == AppContances.TRADE_STATU_CLOSE){
				errMsg = ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1020);
			}
		}

		if (errMsg == null) {
			return true;
		}

		final PopupConfirmDialog dialog = PopupConfirmDialog.newInstance(act, "", errMsg);
		dialog.show();
		return false;
	}

	/**
	 * 下单，平仓时手数的逻辑判断
	 * @param minLot
	 * @param maxLot
	 * @param stepLot
	 * @param lot
     * @return
     */
	public static String getLotErrorMsg(String minLot, String maxLot, String stepLot, String lot) {
//		AppUtil.error("lot = " + lot);
		String errorMessage = "";
		// 如果是最大最小手数，不判断
		if (DoubleConverter.isEqual(DoubleConverter.toFloatData(lot), DoubleConverter.toFloatData(minLot), 2)
				|| DoubleConverter.isEqual(DoubleConverter.toFloatData(lot), DoubleConverter.toFloatData(maxLot), 2)) {
			return errorMessage;
		}
		// 特殊字符判断
		if (lot.isEmpty() || lot.indexOf(".") != -1) {
			String[] lotSplit = lot.split("\\.");
			if (null == lotSplit || lotSplit.length == 0 || "".equals(lotSplit[0])) {
				errorMessage = ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1023);
				return errorMessage;
			}
		}

		//手数大小范围值判断
		float curLot = DoubleConverter.toFloatData(lot);
		if (DoubleConverter.isEqual(curLot, 0, 2)
				|| DoubleConverter.isLT(curLot, DoubleConverter.toFloatData(minLot), 2)
				|| DoubleConverter.isGT(curLot, DoubleConverter.toFloatData(maxLot), 2)) {
			errorMessage = ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1023);
			return errorMessage;
		}

		// 手数步长判断
		float mtempLot = DoubleConverter.toFloat(2, curLot * 100);
		float mUnitLot = DoubleConverter.toFloat(2, DoubleConverter.toFloatData(stepLot) * 100);
		int mModel = (int) (mtempLot % mUnitLot);
		if (!DoubleConverter.isEqual(mModel, 0, 2)) {
			errorMessage = ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1053).replace("%s",stepLot);
		}
		return errorMessage;
	}

	/**
	 * 最大偏移的范围判断
	 * @param maxRange
	 * @param range
     */
	public static String getRangeErrorMsg(String maxRange, String range) {
		String errorMessage = "";

		if (TextUtils.isEmpty(range)) {
			errorMessage = ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1022);
		} else if(DoubleConverter.toIntData(range) > DoubleConverter.toIntData(maxRange) ){
			errorMessage = ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1022);
		}
		return errorMessage;
	}

	/**
	 * 止损点数错误判断
	 * @param minLossPoint
	 * @param maxLossPoint
	 * @param lossPoint
     * @return
     */
	public static String getLossPointErrorMsg(String minLossPoint, String maxLossPoint, String lossPoint){
		return getPriceErrorMsg(minLossPoint,maxLossPoint,lossPoint,ConfigType.SERVER_ERROR_1026);
	}

	/**
	 * 止损点数错误判断
	 * @param minWinPoint
	 * @param maxWinPoint
	 * @param winPoint
	 * @return
	 */
	public static String getWinPointErrorMsg(String minWinPoint, String maxWinPoint, String winPoint){
		return getPriceErrorMsg(minWinPoint,maxWinPoint,winPoint,ConfigType.SERVER_ERROR_1028);
	}

	/**
	 * 止损价格错误判断
	 * @param minLossPrice
	 * @param maxLossPrice
	 * @param lossPrice
	 * @return
	 */
	public static String getLossPriceErrorMsg(String minLossPrice, String maxLossPrice, String lossPrice){
		return getPriceErrorMsg(minLossPrice,maxLossPrice,lossPrice,ConfigType.SERVER_ERROR_1025);
	}

	/**
	 * 止盈价格错误判断
	 * @param minWinPrice
	 * @param maxWinPrice
	 * @param winPrice
	 * @return
	 */
	public static String getWinPriceErrorMsg(String minWinPrice, String maxWinPrice, String winPrice){
		return getPriceErrorMsg(minWinPrice,maxWinPrice,winPrice,ConfigType.SERVER_ERROR_1027);
	}

	/**
	 * 挂单价格错误判断
	 * @param minApplyPrice
	 * @param maxApplyPrice
	 * @param applyPrice
	 * @return
	 */
	public static String getApplyPriceErrorMsg(String minApplyPrice, String maxApplyPrice, String applyPrice){
		return getPriceErrorMsg(minApplyPrice,maxApplyPrice,applyPrice,ConfigType.SERVER_ERROR_1024);
	}

	/**
	 * 挂单价，止损止盈价格及点数范围判断
	 * @param minPrice
	 * @param maxPrice
	 * @param mPrice
	 * @param errorId
	 * @return
	 */
	private static String getPriceErrorMsg(String minPrice , String maxPrice, String mPrice, String errorId){
		String errorMessage = "";
//		Logger.e("minPrice" + minPrice +", mPrice = " + mPrice +", maxPrice = " + maxPrice);
		//空数据
		if (TextUtils.isEmpty(mPrice)) {
			errorMessage = ConfigUtil.instance().getErrorConfigObject().optString(errorId);
		}else if(DoubleConverter.toFloatData(mPrice) < DoubleConverter.toFloatData(minPrice) ){
			//小于最小值
			errorMessage = ConfigUtil.instance().getErrorConfigObject().optString(errorId);
		}else if(DoubleConverter.toFloatData(mPrice) > DoubleConverter.toFloatData(maxPrice) ){
			//大于最大值
			errorMessage = ConfigUtil.instance().getErrorConfigObject().optString(errorId);
		}

		return errorMessage;
	}

	/**
	 * 市价下单界面 根据值获取止损止盈范围值
	 * @param mTickModel 报价对象
	 * @param mSymbolModel 产品对象
	 * @param mRangeModel 范围值对象
	 * @param mTradeDir 买卖方向
     */
	public static void calLossAndWin(DataItemDetail mTickModel, DataItemDetail mSymbolModel, TradeRangeModel mRangeModel, int mTradeDir){
		if(null == mTickModel || null == mSymbolModel){
			return;
		}
		mRangeModel.mTradeDir = mTradeDir;
		mRangeModel.setData(mSymbolModel);
		double minPrice = DoubleConverter.toDoubleData(mSymbolModel.getString(GTSConst.JSON_KEY_MINPRICE));
		double maxPrice = DoubleConverter.toDoubleData(mSymbolModel.getString(GTSConst.JSON_KEY_MAXPRICE));

		//买入方向，止损取大，止盈取小
		//买入价格 - 最大 <= 止损 <= 买入价格 - 最小   取右边
		//买入价格 + 最小 <= 止盈 <= 买入价格 + 最大   取左边
		if(mTradeDir == AppContances.TRADE_TYPE_BUY){
			mRangeModel.mSysPrice = mTickModel.getString(GTSConst.JSON_KEY_BUYPRICE);
			mRangeModel.minRangeLoss = calReducePrice(DoubleConverter.toDoubleData(mRangeModel.mSysPrice), maxPrice, mRangeModel.mDigit);/**买方向止损左边范围 */
			mRangeModel.maxRangeLoss = calReducePrice(DoubleConverter.toDoubleData(mRangeModel.mSysPrice), minPrice, mRangeModel.mDigit);/**买方向止损右边范围 */
			mRangeModel.minRangeWin = calPlusPrice(DoubleConverter.toDoubleData(mRangeModel.mSysPrice), minPrice, mRangeModel.mDigit); /**买方向止盈左边范围  */
			mRangeModel.maxRangeWin = calPlusPrice(DoubleConverter.toDoubleData(mRangeModel.mSysPrice), maxPrice, mRangeModel.mDigit);/**买方向止盈右边范围  */
			mRangeModel.defalutLossPrice = mRangeModel.maxRangeLoss;
			mRangeModel.defalutWinPrice = mRangeModel.minRangeWin;
		}else{
			//卖出方向，止损取小，止盈取大
			//卖出价格 + 最小 <= 止损 <= 卖出价格 + 最大  取左边
			//卖出价格 - 最大 <= 止盈 <= 卖出价格 - 最小   取右边
			mRangeModel.mSysPrice = mTickModel.getString(GTSConst.JSON_KEY_SELLPRICE);
			mRangeModel.minRangeLoss = calPlusPrice(DoubleConverter.toDoubleData(mRangeModel.mSysPrice), minPrice, mRangeModel.mDigit);/**卖方向止损左边范围 */
			mRangeModel.maxRangeLoss = calPlusPrice(DoubleConverter.toDoubleData(mRangeModel.mSysPrice), maxPrice, mRangeModel.mDigit);/**卖方向止损右边范围  */
			mRangeModel.minRangeWin = calReducePrice(DoubleConverter.toDoubleData(mRangeModel.mSysPrice), maxPrice, mRangeModel.mDigit);/**卖方向止损左边范围  */
			mRangeModel.maxRangeWin = calReducePrice(DoubleConverter.toDoubleData(mRangeModel.mSysPrice), minPrice, mRangeModel.mDigit);/**卖方向止盈右边范围  */
			mRangeModel.defalutLossPrice = mRangeModel.minRangeLoss;
			mRangeModel.defalutWinPrice = mRangeModel.maxRangeWin;
		}
	}

	/**
	 * 设置止损止盈界面 根据值获取止损止盈范围值
	 * @param mTickModel 报价对象
	 * @param mSymbolModel 产品对象
	 * @param mRangeModel 范围值对象
	 * @param mTradeDir 买卖方向
	 */
	public static void calLossAndWin2(DataItemDetail mTickModel, DataItemDetail mSymbolModel, TradeRangeModel mRangeModel, int mTradeDir){
		if(null == mTickModel || null == mSymbolModel){
			return;
		}
		mRangeModel.mTradeDir = mTradeDir;
		mRangeModel.setData(mSymbolModel);
		double minPrice = DoubleConverter.toDoubleData(mSymbolModel.getString(GTSConst.JSON_KEY_MINPRICE));
		double maxPrice = DoubleConverter.toDoubleData(mSymbolModel.getString(GTSConst.JSON_KEY_MAXPRICE));

		//买入方向，止损取大，止盈取小
		//卖出价格 - 最大 <= 止损 <= 卖出价格 - 最小   取右边
		//卖出价格 + 最小 <= 止盈 <= 卖出价格 + 最大   取左边
		if(mTradeDir == AppContances.TRADE_TYPE_BUY){
			mRangeModel.mSysPrice = mTickModel.getString(GTSConst.JSON_KEY_SELLPRICE);
			mRangeModel.minRangeLoss = calReducePrice(DoubleConverter.toDoubleData(mRangeModel.mSysPrice), maxPrice, mRangeModel.mDigit);/**买方向止损左边范围 */
			mRangeModel.maxRangeLoss = calReducePrice(DoubleConverter.toDoubleData(mRangeModel.mSysPrice), minPrice, mRangeModel.mDigit);/**买方向止损右边范围 */
			mRangeModel.minRangeWin = calPlusPrice(DoubleConverter.toDoubleData(mRangeModel.mSysPrice), minPrice, mRangeModel.mDigit); /**买方向止盈左边范围  */
			mRangeModel.maxRangeWin = calPlusPrice(DoubleConverter.toDoubleData(mRangeModel.mSysPrice), maxPrice, mRangeModel.mDigit);/**买方向止盈右边范围  */
			mRangeModel.defalutLossPrice = mRangeModel.maxRangeLoss;
			mRangeModel.defalutWinPrice = mRangeModel.minRangeWin;
		}else{
			//卖出方向，止损取小，止盈取大
			//买入价格 + 最小 <= 止损 <= 买入价格 + 最大  取左边
			//买入价格 - 最大 <= 止盈 <= 买入价格 - 最小   取右边
			mRangeModel.mSysPrice = mTickModel.getString(GTSConst.JSON_KEY_BUYPRICE);
			mRangeModel.minRangeLoss = calPlusPrice(DoubleConverter.toDoubleData(mRangeModel.mSysPrice), minPrice, mRangeModel.mDigit);/**卖方向止损左边范围 */
			mRangeModel.maxRangeLoss = calPlusPrice(DoubleConverter.toDoubleData(mRangeModel.mSysPrice), maxPrice, mRangeModel.mDigit);/**卖方向止损右边范围  */
			mRangeModel.minRangeWin = calReducePrice(DoubleConverter.toDoubleData(mRangeModel.mSysPrice), maxPrice, mRangeModel.mDigit);/**卖方向止损左边范围  */
			mRangeModel.maxRangeWin = calReducePrice(DoubleConverter.toDoubleData(mRangeModel.mSysPrice), minPrice, mRangeModel.mDigit);/**卖方向止盈右边范围  */
			mRangeModel.defalutLossPrice = mRangeModel.minRangeLoss;
			mRangeModel.defalutWinPrice = mRangeModel.maxRangeWin;
		}
	}

	/**
	 * 市价挂单根据值计算挂单界面止损止盈范围值
	 * @param mTickModel 报价对象
	 * @param mRangeModel 范围值对象
	 * @param mSymbolModel 产品对象
	 * @param inputPrice 挂单价格
	 * @param mTradeDir 买卖方向
	 * @param priceType 挂单类型，限价1 or 停损2
	 */
	public static void calPendingLossAndWin(DataItemDetail mTickModel, DataItemDetail mSymbolModel, TradeRangeModel mRangeModel, String inputPrice, int mTradeDir, int priceType){
		if(null == mTickModel || null == mSymbolModel){
			return;
		}
		mRangeModel.mTradeDir = mTradeDir;
		mRangeModel.mSysPrice = inputPrice;
		mRangeModel.setData(mSymbolModel);

		// 设置价格值范围
		double curPrice = DoubleConverter.toDoubleData(inputPrice);
		double minPrice = DoubleConverter.toDoubleData(mSymbolModel.getString(GTSConst.JSON_KEY_MINPRICE));
		double maxPrice = DoubleConverter.toDoubleData(mSymbolModel.getString(GTSConst.JSON_KEY_MAXPRICE));
		//买入方向，
		if(mTradeDir == AppContances.TRADE_TYPE_BUY){
			String mPrice = mTickModel.getString(GTSConst.JSON_KEY_BUYPRICE);
			//限价挂单，取止损范围，默认取大
			//买入价格 - 最大 <= 止损 <= 买入价格 - 最小   取右边
			if(priceType == AppContances.ORDER_TYPE_APPLY_TYPE){
				mRangeModel.minRangePrice = calReducePrice(DoubleConverter.toDoubleData(mPrice), maxPrice, mRangeModel.mDigit);
				mRangeModel.maxRangePrice = calReducePrice(DoubleConverter.toDoubleData(mPrice), minPrice, mRangeModel.mDigit);
				mRangeModel.defalutPrice = mRangeModel.maxRangePrice;
			}else{
				//停损挂单，取止盈范围，默认取小
				//买入价格 + 最小 <= 止盈 <= 买入价格 + 最大   取左边
				mRangeModel.minRangePrice = calPlusPrice(DoubleConverter.toDoubleData(mPrice), minPrice, mRangeModel.mDigit);
				mRangeModel.maxRangePrice = calPlusPrice(DoubleConverter.toDoubleData(mPrice), maxPrice, mRangeModel.mDigit);
				mRangeModel.defalutPrice = mRangeModel.minRangePrice;
			}

			//挂单价格 - 最大 <= 止损 <= 挂单价格 - 最小   取大
			mRangeModel.minRangeLoss = calReducePrice(curPrice, maxPrice, mRangeModel.mDigit);
			mRangeModel.maxRangeLoss = calReducePrice(curPrice, minPrice, mRangeModel.mDigit);

			//挂单价格 + 最小 <= 止盈 <= 挂单价格 + 最大   取小
			mRangeModel.minRangeWin = calPlusPrice(curPrice, minPrice, mRangeModel.mDigit);
			mRangeModel.maxRangeWin = calPlusPrice(curPrice, maxPrice, mRangeModel.mDigit);

			mRangeModel.defalutLossPrice = mRangeModel.maxRangeLoss;
			mRangeModel.defalutWinPrice = mRangeModel.minRangeWin;
		}else{
			String mPrice = mTickModel.getString(GTSConst.JSON_KEY_SELLPRICE);
			//限价挂单，取止损范围，默认取小
			//卖出价格 + 最小 <= 止损 <= 卖出价格 + 最大  取左边
			if(priceType == AppContances.ORDER_TYPE_APPLY_TYPE){
				mRangeModel.minRangePrice = calPlusPrice(DoubleConverter.toDoubleData(mPrice), minPrice, mRangeModel.mDigit);
				mRangeModel.maxRangePrice = calPlusPrice(DoubleConverter.toDoubleData(mPrice), maxPrice, mRangeModel.mDigit);
				mRangeModel.defalutPrice = mRangeModel.minRangePrice;
			}else{
				//停损挂单，取止盈范围，默认取大
				//卖出价格 - 最大 <= 止盈 <= 卖出价格 - 最小   取右边
				mRangeModel.minRangePrice = calReducePrice(DoubleConverter.toDoubleData(mPrice), maxPrice, mRangeModel.mDigit);
				mRangeModel.maxRangePrice = calReducePrice(DoubleConverter.toDoubleData(mPrice), minPrice, mRangeModel.mDigit);
				mRangeModel.defalutPrice = mRangeModel.maxRangePrice;
			}

			//挂单价格 + 最小 <= 止损 <= 挂单价格 + 最大  取小
			mRangeModel.minRangeLoss = calPlusPrice(curPrice, minPrice, mRangeModel.mDigit);
			mRangeModel.maxRangeLoss =calPlusPrice(curPrice, maxPrice, mRangeModel.mDigit);

			//挂单价格 - 最大 <= 止盈 <= 挂单价格 - 最小   取大
			mRangeModel.minRangeWin = calReducePrice(curPrice, maxPrice, mRangeModel.mDigit);
			mRangeModel.maxRangeWin = calReducePrice(curPrice, minPrice, mRangeModel.mDigit);

			mRangeModel.defalutLossPrice = mRangeModel.minRangeLoss;
			mRangeModel.defalutWinPrice = mRangeModel.maxRangeWin;
		}
	}

	/**
	 * 计算相加的两个价格值
	 * @param curPrice
	 * @param plusPrice
	 * @param digit
     * @return
     */
	private static String calPlusPrice(double curPrice, double plusPrice, int digit){
		String price = "";
//		Logger.e("toStringData curPrice == " + curPrice + ", " + plusPrice +", " + DoubleConverter.add(curPrice,plusPrice));
		price = DoubleConverter.toStringData(DoubleConverter.add(curPrice,plusPrice), digit);
		return price;
	}

	/**
	 * 计算相减的两个价格值
	 * @param curPrice
	 * @param reducePrice
	 * @param digit
     * @return
     */
	private static String calReducePrice(double curPrice, double reducePrice, int digit){
		String price = "";
//		Logger.e("toStringData curPrice == " + curPrice + ", " + reducePrice +", " + DoubleConverter.sub(curPrice,reducePrice));
		price = DoubleConverter.toStringData(DoubleConverter.sub(curPrice,reducePrice), digit);
		return price;
	}

	/**
	 * 止损价，点转换成价格
	 *
	 * @param curPoint
	 */
	public static String lossPointToPrice(String curPoint, TradeRangeModel mRangeModel) {
		if (curPoint.isEmpty()) {
			return "0";
		}
		double mPrice = 0;
		double mCurPoint = DoubleConverter.toDoubleData(curPoint);
		double mSysPrice = DoubleConverter.toDoubleData(mRangeModel.mSysPrice);
        //点-->价, 买入方向止损, 当前系统价 - 输入点数
		if (mRangeModel.mTradeDir == AppContances.TRADE_TYPE_BUY) {
			mPrice = DoubleConverter.toDouble(mRangeModel.mDigit,
					mSysPrice - DoubleConverter.toDouble(mRangeModel.mDigit, mCurPoint * mRangeModel.pointToPrice));
		} else {
			//点-->价, 卖出方向止损, 当前系统价 + 输入点数
			mPrice = DoubleConverter.toDouble(mRangeModel.mDigit,
					mSysPrice + DoubleConverter.toDouble(mRangeModel.mDigit, mCurPoint * mRangeModel.pointToPrice));
		}

		Logger.e("止损点转换成价格 mCurPoint = " + mCurPoint + ", mSysPrice = " + mSysPrice +", mPrice " + mPrice +", " + mRangeModel.mDigit);

		return DoubleConverter.toStringData(mPrice, mRangeModel.mDigit);
	}

	/**
	 * 止盈价，点转换成价格
	 *
	 * @param curPoint
	 */
	public static String profitPointToPrice(String curPoint, TradeRangeModel mRangeModel) {
		if (curPoint.isEmpty()) {
			return "0";
		}
		double mPrice = 0f;
		double mCurPoint = DoubleConverter.toDoubleData(curPoint);
		double mSysPrice = DoubleConverter.toDoubleData(mRangeModel.mSysPrice);
        //点-->价, 买入方向止盈, 当前系统价 + 输入点数
		if (mRangeModel.mTradeDir == AppContances.TRADE_TYPE_BUY) {
			mPrice = DoubleConverter.toDouble(mRangeModel.mDigit,
					mSysPrice + DoubleConverter.toDouble(mRangeModel.mDigit, mCurPoint * mRangeModel.pointToPrice));
		} else {
			//点-->价, 卖出方向止盈, 当前系统价 - 输入点数
			mPrice = DoubleConverter.toDouble(mRangeModel.mDigit,
					mSysPrice - DoubleConverter.toDouble(mRangeModel.mDigit, mCurPoint * mRangeModel.pointToPrice));
		}

		Logger.e("止盈点转换成价格 mCurPoint = " + mCurPoint + ", mSysPrice = " + mSysPrice +", mPrice " + mPrice + ", " + mRangeModel.mDigit);

		return DoubleConverter.toStringData(mPrice, mRangeModel.mDigit);
	}

	/**
	 * 止损价，价格转换成点
	 *
	 * @param curPrice
	 */
	public static String lossPriceToPoint(String curPrice, TradeRangeModel mRangeModel) {
		if (curPrice.isEmpty()) {
			return "0";
		}
		double mPoint = 0;
		double mCurPrice = DoubleConverter.toDoubleData(curPrice);
		double mSysPrice = DoubleConverter.toDoubleData(mRangeModel.mSysPrice);
		int digit = mRangeModel.mDigit - mRangeModel.stepPrice;

        //价-->点, 买入方向止损, 当前系统价 - 输入价格
		if (mRangeModel.mTradeDir == AppContances.TRADE_TYPE_BUY) {
			mPoint =  DoubleConverter.toDouble(digit, mSysPrice * mRangeModel.priceToPoint)
					- DoubleConverter.toDouble(digit, mCurPrice * mRangeModel.priceToPoint);
		} else {
			//价-->点, 卖出方向止损, 输入价格 - 当前系统价
			mPoint = DoubleConverter.toDouble(digit, mCurPrice * mRangeModel.priceToPoint)
					-  DoubleConverter.toDouble(digit, mSysPrice * mRangeModel.priceToPoint);
		}

		Logger.e("止损价格转换成点数 curPrice = " + curPrice + ", mSysPrice = " + mSysPrice +", mPoint " + mPoint + ", " + (mRangeModel.mDigit - mRangeModel.stepPrice));

		return DoubleConverter.toStringData(mPoint, mRangeModel.mDigit - mRangeModel.stepPrice);
	}

	/**
	 * 止盈价，价格转换成点
	 *
	 * @param curPrice
	 */
	public static String profitPriceToPoint(String curPrice, TradeRangeModel mRangeModel) {
		if (curPrice.isEmpty()) {
			return "0";
		}
		double mPoint = 0f;
		double mCurPrice = DoubleConverter.toDoubleData(curPrice);
		double mSysPrice = DoubleConverter.toDoubleData(mRangeModel.mSysPrice);
		int digit = mRangeModel.mDigit - mRangeModel.stepPrice;

        //价-->点, 买入方向止盈, 输入价格 - 当前系统价
		if (mRangeModel.mTradeDir == AppContances.TRADE_TYPE_BUY) {
			mPoint = DoubleConverter.toDouble(digit, mCurPrice * mRangeModel.priceToPoint)
					- DoubleConverter.toDouble(digit, mSysPrice * mRangeModel.priceToPoint);
		} else {
			//价-->点, 卖出方向止盈, 当前系统价 - 输入价格
			mPoint = DoubleConverter.toDouble(digit, mSysPrice * mRangeModel.priceToPoint)
					- DoubleConverter.toDouble(digit, mCurPrice * mRangeModel.priceToPoint);
		}

		Logger.e("止盈价格转换成点数 curPrice = " + curPrice + ", mSysPrice = " + mSysPrice +", mPoint " + mPoint + ", " + (mRangeModel.mDigit - mRangeModel.stepPrice));

		return DoubleConverter.toStringData(mPoint, mRangeModel.mDigit - mRangeModel.stepPrice);
	}
}
